#!/bin/bash

DOCKER_BASE_IMAGE_NAME="local_base"
DOCKER_BASE_IMAGE_TAG="v0"
DOCKER_INCR_IMAGE_NAME="local_dev_env"
DOCKER_INCR_IMAGE_TAG="x1"
DOCKER_INST_NAME="local_dev_env"

image_count=`sudo docker images |grep $DOCKER_BASE_IMAGE_NAME|wc -l`
if [[ $image_count -eq 0 ]]; then
    sudo docker build -f Dockerfile.base -t ${DOCKER_BASE_IMAGE_NAME}:${DOCKER_BASE_IMAGE_TAG} .
fi
running_inst=`sudo docker ps |grep $DOCKER_INST_NAME |wc -l`
if [[ $running_inst -eq 0 ]];then
    echo "no INSTANCE $DOCKER_INST_NAME running"
else
    sudo docker stop $DOCKER_INST_NAME
fi
sudo docker build -f Dockerfile.incremental -t ${DOCKER_INCR_IMAGE_NAME}:${DOCKER_INCR_IMAGE_TAG} .
sudo docker run --rm -d -p 8080:8080 -p 2222:22 --name $DOCKER_INST_NAME ${DOCKER_INCR_IMAGE_NAME}:${DOCKER_INCR_IMAGE_TAG}
